package com.hboxs.asl.util.sms;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 谐和科技
 * Created by will on 2015/9/11.
 */
public class XieHeKeJiSms {

    private static final String URL_BATCH_SEND = "http://webservice.wjxjt.com:8080/sms/sendsms.jsp";
    private static final String ACCOUNT = "bubu@bubu";
    private static final String PASSWORD = "w2J44RNG";
    /**
     * 拓展码，必须是数字字符串。若不需要拓展，则留空
     */
    private static final String EXPANDNO = "";
    /**
     * 批次号，发送批次的唯一标识。用户可根据自己的业务规则定义批次号， 批次号长度不能超过 50个字符。若留空，则每次发送返回的批次号由接 口 生成，为 36位的 UUID字符串。
     */
    private static final String BATCHNO = "";

    public static final String KEY_CODE = "code";
    public static final String KEY_BATCH_NO = "batchno";
    public static final String KEY_MESSAGE = "message";

    /**
     * 群发接口
     * @param content 短信内容，最大不超过 600个字符。中英文字符均按一个字符计算
     * @param mobiles  群发号码，多个号码以英文逗号分隔，号码总数不超过 500 个
     * @return
     */
    public static Map<String, Object> batchSend(String content, String mobiles) {
        Map<String, Object> map = new HashMap<>();

        try {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(URL_BATCH_SEND);

            List<NameValuePair> nvps = new ArrayList<>();
            nvps.add(new BasicNameValuePair("account", ACCOUNT));
            nvps.add(new BasicNameValuePair("passwd", PASSWORD));
            nvps.add(new BasicNameValuePair("mobiles", mobiles));
            nvps.add(new BasicNameValuePair("content", content));
            nvps.add(new BasicNameValuePair("expandno", EXPANDNO));
            nvps.add(new BasicNameValuePair("batchno", ""));
            nvps.add(new BasicNameValuePair("digest", DigestUtils.md5Hex(mobiles + content)));

            post.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));

            HttpResponse response = client.execute(post);

            HttpEntity entity = response.getEntity();

            String body = EntityUtils.toString(entity);
            System.out.println(body);

            JSONObject jsonObject = new JSONObject(body);

            int code = -1;
            String batchno = "";

            if(jsonObject.has(KEY_CODE)) code = jsonObject.getInt(KEY_CODE);
            if(jsonObject.has(KEY_BATCH_NO)) batchno = jsonObject.getString(KEY_BATCH_NO);

            map.put(KEY_CODE, code);
            map.put(KEY_BATCH_NO, batchno);
            map.put(KEY_MESSAGE, resultDescription(code));

            post.releaseConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }


    private static String resultDescription(int code) {
        switch (code){
            case 200:
                return "短信发送成功";
            case 301:
                return "账号或密码错误";
            case 302:
                return "账号已停用";
            case 303:
                return "余额不足";
            case 304:
                return "手机号码为空或者超过最大发送数量";
            case 305:
                return "数据认证错误";
            case 306:
                return "短信内容为空或超长";
            case 307:
                return "扩展码不正确";
            case 308:
                return "批次号过长";
            case 309:
                return "含有敏感词";
            case 310:
                return "非模版内容";
            case 998:
                return "响应超时";
            case 999:
                return "其他未知错误";
            default:
                return "未知错误";
        }
    }

}
